/*
* Mentioned DB1 as your Compared Database here
*/
;WITH 
SrcCols AS (
               SELECT c.Object_id, o.Name AS TableName, c.Column_id, C.Name AS ColName
               FROM   mainserver.DB1.sys.Columns c
                      INNER JOIN mainserver.DB1.sys.Tables o
                           ON  o.OBJECT_ID = c.OBJECT_ID
           ),
Srcdef AS (
              SELECT o.Name AS TableName, c.name AS ColName, d.* 
              FROM   mainserver.DB1.sys.default_constraints d
                     INNER JOIN mainserver.DB1.sys.Objects o
                          ON  d.parent_object_id = o.Object_id
                     INNER JOIN mainserver.DB1.Sys.columns c
                          ON  c.Object_id = o.OBJECT_ID AND c.Column_id = d.parent_column_id
          ),
DesCols AS (
               SELECT c.Object_id, o.Name AS TableName, c.Column_id, C.Name AS ColName
               FROM   sys.Columns c 
                      INNER JOIN sys.Tables o
                           ON  o.OBJECT_ID = c.OBJECT_ID
           ),
Desdef AS(
             SELECT object_name (o.parent_object_id) AS TableName, col_name (o.parent_object_id, o.parent_column_id) AS 
                    ColName, *
             FROM   sys.default_constraints o
         )	 
SELECT 'IF OBJECT_id('''+s.NAME+''') IS NULL
ALTER TABLE ' + s.tableName + ' ADD CONSTRAINT '+s.NAME +' DEFAULT '+s.Definition + ' FOR ' + s.colName +'
GO'AS Script
FROM   srcdef s
       LEFT OUTER JOIN DESdef d
            ON  s.TableName = d.Tablename AND s.ColName = d.colName
WHERE d.tablename IS NULL AND EXISTS(SELECT * FROM descols Dc WHERE dc.colname =s.ColName AND dc.Tablename = s.TableName)
  
